package com.chuji.crmcj.act;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.OrderAdapter;
import com.chuji.crmcj.adapter.OrderListAdapter;
import com.chuji.crmcj.fragment.BaseFragment;
import com.chuji.crmcj.fragment.FullPayFragment;
import com.chuji.crmcj.fragment.OrderFragment;
import com.nineoldandroids.view.ViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by w.pitt on 2016/1/15.
 */
public class OrderListAct extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ListView mLvClientOrder;
    private Button mBtFullPay;
    private Button mBtOrder;
    private int screenW;
    private View indicateLine;
    private OrderAdapter orderAdapter;
    private ViewPager mVpOrder;
    private View ll_left;

    @Override
    protected void initView() {
        setContentView(R.layout.act_order_client);
//        mLvClientOrder = (ListView) findViewById(R.id.lv_client_order);
        mBtOrder = (Button) findViewById(R.id.btn_order);
        mBtFullPay = (Button) findViewById(R.id.btn_full_pay);
        indicateLine = findViewById(R.id.indicate_line);
        mVpOrder = (ViewPager) findViewById(R.id.vp_order);
        ll_left = findViewById(R.id.ll_left);
    }

    List<BaseFragment> fragments;

    @Override
    protected void initData() {
        fragments = new ArrayList<BaseFragment>();
        fragments.add(new OrderFragment());
        fragments.add(new FullPayFragment());
//        orderAdapter = new FullPayAdapter(this, mLvClientOrder, mDataLeft, lable);
//        mLvClientOrder.setAdapter(orderAdapter);
        OrderListAdapter myPageAdapter = new OrderListAdapter(getSupportFragmentManager(), fragments);
        mVpOrder.setAdapter(myPageAdapter);

        updateTabs(0);

        screenW = getWindowManager().getDefaultDisplay().getWidth();
        indicateLine.getLayoutParams().width = screenW / 2;
        indicateLine.requestLayout(); // 重新计算大小，并刷新控件

        mTvBarTitle.setText("订单客户");
    }

    @Override
    protected void initListener() {
        ll_left.setOnClickListener(this);
        mBtFullPay.setOnClickListener(this);
        mBtOrder.setOnClickListener(this);
        mVpOrder.setOnPageChangeListener(new OnMainPageChangeListener());
    }

    private int lable = 0;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_left:
                finish();
                break;
            case R.id.btn_order:
                mVpOrder.setCurrentItem(0);
                break;
            case R.id.btn_full_pay:
                mVpOrder.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(this, OrderDetialAct.class);
        intent.putExtra("SOURCE", lable);
        startActivity(intent);
    }

    private void updateTabs(int position) {
        updateTab(position, 0, mBtOrder);
        updateTab(position, 1, mBtFullPay);
    }

    private void updateTab(int position, int tabPosition, TextView tab) {
        int blue = getResources().getColor(R.color.qianlan);
        int halfWhite = getResources().getColor(R.color.font2);
        if (position == tabPosition) {
            // tab对应的页面被选中
            tab.setTextColor(blue);
        } else {
            // tab对应的页面没有被选中
            tab.setTextColor(halfWhite);
        }
    }

    private class OnMainPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        /** 当touch事件发生时回调此方法 */
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // 偏移位置 = 手指划过屏幕的百分比 * 指示器宽度
            int offsetX = (int) (positionOffset * indicateLine.getWidth());
            // 起始位置 = position * 指示器宽度
            int startX = position * indicateLine.getWidth();
            // 指示器移动的位置 = 起始位置 + 偏移位置
            int translationX = startX + offsetX;
            ViewHelper.setTranslationX(indicateLine, translationX);
        }

        @Override
        /** 当见面选中状态发生变更时会回调此方法 */
        public void onPageSelected(int position) {
            // 高亮选中页面对应的标签，并将其他的变暗
            updateTabs(position);
        }

        @Override
        /** 当页面的滑动状态发生变更会回调此方法 */
        public void onPageScrollStateChanged(int state) {

        }
    }
}
